package com.concurrent.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author: zhouyang
 * @descrpition: 实现指定时间内做一定事情
 * @time: 2019-05-08 11:19
 */
@Slf4j
public class CountDownLatchTest {
    private static int clientTotal = 2000;

    public static void main(String[] args) throws Exception{
        ExecutorService executorService = Executors.newCachedThreadPool();

        final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);

        for(int i=0;i<clientTotal;++i){
            final int now = i;
            executorService.execute(()->{
                try {
                    test(now);
                }catch (Exception e){
                    log.error(e.toString());
                }finally {
                    countDownLatch.countDown();
                }

            });
        }
        countDownLatch.await(2, TimeUnit.SECONDS);
        log.info("thread finish");
        executorService.shutdown();
    }

    private static void test(final int threadNum) throws Exception{
        Thread.sleep(3000);
        log.info("thread {}",threadNum);
    }
}
